home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / WIN.C < prev    next >
C/C++ Source or Header  |  1989-05-21  |  31KB  |  1,241 lines

  1. /*******************************************************************************/
  2. /*                                           */
  3. /*  Win.C                                       */
  4. /*                                           */
  5. /*  Written By Jeff McElroy 6/21/89                           */
  6. /*                                           */
  7. /*******************************************************************************/
  8.  
  9. #include <malloc.h>
  10. #include <memory.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "win.h"
  14.  
  15. /*******************************************************************************/
  16. /* Curses Routines Available                               */
  17. /*******************************************************************************/
  18. #define ANSI_SEQ        0    /* Use Ansi Sequences               */
  19. #define OS2_V12         0    /* Use OS2 v1.2 Kernal               */
  20.                     /* This Stays within FAPI           */
  21. #define DOS_BIOS        0    /* Use Bios Int 10h               */
  22. #define DOS_DIRECT        1    /* Write Directly Using Bios to Figure */
  23.                     /* The Screen buffer's Location        */
  24. /*******************************************************************************/
  25. /* Frame Characters Set's Available                                            */
  26. /*******************************************************************************/
  27. #define IBM_EXTENDED        1    /* Use IBM Extended Ascii Codes        */
  28. #define STANDARD_ASCII        0    /* Use Standard Ascii Codes           */
  29. /*******************************************************************************/
  30. /* Constants                                       */
  31. /*******************************************************************************/
  32.  
  33. #if IBM_EXTENDED
  34. /*******************************************************************************/
  35. /*  IBM EXTENDED ASCII SET                               */
  36. /*******************************************************************************/
  37.     #define HORIZONTAL_CHAR        '\xc4'
  38.                     /* Character used for the horizontal   */
  39.                     /* bar in each window's frame.         */
  40.     #define VERTICAL_CHAR        '\xba'
  41.                     /* Character used for the vertical bar */
  42.                     /* in each window's frame.             */
  43.     #define UPPER_LEFT_CHAR        '\xd6'
  44.                     /* Character used as the upper left    */
  45.                     /* corner in each window's frame.      */
  46.     #define LOWER_LEFT_CHAR        '\xd3'
  47.                     /* Character used as the lower left    */
  48.                     /* corner in each window's frame.      */
  49.     #define UPPER_RIGHT_CHAR        '\xb7'
  50.                     /* Character used as the upper right   */
  51.                     /* corner in each window's frame.      */
  52.     #define LOWER_RIGHT_CHAR        '\xbd'
  53.                     /* Character used as the lower right   */
  54.                     /* corner in each window's frame.      */
  55.     #define BACKSPACE_CHAR        '\b'
  56.                     /* BackSpace Character               */
  57.     #define TAB_CHAR            '\t'
  58.                     /* Tab Character               */
  59.     #define LINEFEED_CHAR        '\n'
  60.                     /* Line Feed Character               */
  61.     #define RETURN_CHAR         '\r'
  62.                     /* Return Character               */
  63.     #define BELL_CHAR            '\a'
  64.                     /* Bell Character               */
  65.     #define SPACE_CHAR            ' '
  66.                     /* Space Character               */
  67.  
  68.     #define DEFAULT_TAB         8
  69.                     /* Default Tabs                */
  70.     #define DEFAULT_BACKGROUNDCHAR  SPACE_CHAR
  71.                     /* Default BackGround Character        */
  72. /*******************************************************************************/
  73. #elif STANDARD_ASCII
  74. /*******************************************************************************/
  75. /*  STANDARD ASCII SET                                   */
  76. /*******************************************************************************/
  77.  
  78.     #define HORIZONTAL_CHAR        '-'
  79.                     /* Character used for the horizontal   */
  80.                     /* bar in each window's frame.         */
  81.     #define VERTICAL_CHAR        '|'
  82.                     /* Character used for the vertical bar */
  83.                     /* in each window's frame.             */
  84.     #define UPPER_LEFT_CHAR        '/'
  85.                     /* Character used as the upper left    */
  86.                     /* corner in each window's frame.      */
  87.     #define LOWER_LEFT_CHAR        '\\'
  88.                     /* Character used as the lower left    */
  89.                     /* corner in each window's frame.      */
  90.     #define UPPER_RIGHT_CHAR        '\\'
  91.                     /* Character used as the upper right   */
  92.                     /* corner in each window's frame.      */
  93.     #define LOWER_RIGHT_CHAR        '/'
  94.                     /* Character used as the lower right   */
  95.                     /* corner in each window's frame.      */
  96.     #define BACKSPACE_CHAR        '\b'
  97.                     /* BackSpace Character               */
  98.     #define TAB_CHAR            '\t'
  99.                     /* Tab Character               */
  100.     #define LINEFEED_CHAR        '\n'
  101.                     /* Line Feed Character               */
  102.     #define RETURN_CHAR         '\r'
  103.                     /* Return Character               */
  104.     #define BELL_CHAR            '\a'
  105.                     /* Bell Character               */
  106.     #define SPACE_CHAR            ' '
  107.                     /* Space Character               */
  108. /*******************************************************************************/
  109. #else
  110.     #error Must Select Character Set To Use
  111. #endif
  112.  
  113. #define DEFAULT_BACKGROUNDCHAR    SPACE_CHAR
  114.                     /* Default BackGround Character        */
  115. #define DEFAULT_TAB        8    /* Default Tabs                */
  116.  
  117. /*******************************************************************************/
  118. /* Defintition Of Character Cells                           */
  119. /*******************************************************************************/
  120. struct CHARCELL
  121. {
  122.     unsigned char ucChar;        /* Character                   */
  123.     enum eWinColors eForeAttr;        /* Foreground attribute            */
  124.     enum eWinColors eBackAttr;        /* Background attribute            */
  125. };
  126. /*******************************************************************************/
  127. /* Defintition Windows                                   */
  128. /* All Origins at ( 0,0 )                               */
  129. /*******************************************************************************/
  130. struct WINDOW
  131. {
  132.     unsigned long ulFlags;        /* Type and current status of window   */
  133.     short sViewX, sViewY;        /* Location of the upper left corner   */
  134.                     /* corner of the window's viewport     */
  135.                     /* relative of the screen origin.      */
  136.     short sPhysX, sPhysY;        /* Location of the upper left corner   */
  137.                     /* of the window relative of the       */
  138.                     /* screen origin.               */
  139.     short sCursX, sCursY;        /* Location of the window's cursor     */
  140.                     /* in the window's data buffer.        */
  141.     short sOrigX, sOrigY;        /* Location of the upper left corner   */
  142.                     /* of the window's viewport in the     */
  143.                     /* window's data buffer.               */
  144.     unsigned short usTabStop;        /* Number of space each tab sent to    */
  145.                     /* this window represents.           */
  146.     unsigned short usPhysW, usPhysL;    /* The Physical dimensions of the      */
  147.                     /* window.                   */
  148.     unsigned short usViewW, usViewL;    /* The physical dimensions of the      */
  149.                     /* windows viewport.               */
  150.     unsigned short usLogcW, usLogcL;    /* The dimensions of the window's data */
  151.                     /* buffer.                   */
  152.     struct CHARCELL *pData;        /* Pointer to the window's data buffer.*/
  153.     unsigned char ucBackGroundChar;    /* Character to use when the view port */
  154.                     /* exceeds the window's data buffer.   */
  155.     enum eWinColors eForeAttr;        /* Current foreground attribute for    */
  156.                     /* the window's viewport.              */
  157.     enum eWinColors eBackAttr;        /* Current background attribute for    */
  158.                     /* the window's viewport.              */
  159.     enum eWinColors eForeBord;        /* Foreground attribute for the        */
  160.                     /* window's border.                    */
  161.     enum eWinColors eBackBord;        /* Background attribute for the        */
  162.                     /* window's border.                    */
  163. };
  164.  
  165. unsigned char ucBackGroundChar;
  166. enum eWinColors eForeBackGrnd;
  167. enum eWinColors eBackBackGrnd;
  168.  
  169. enum eWinColors eForeShadow;
  170. enum eWinColors eBackShadow;
  171.  
  172.  
  173. unsigned short usScreenWidth;
  174. unsigned short usScreenLength;
  175.  
  176. short sScreenOriginX;
  177. short sScreenOriginY;
  178.  
  179. struct WINDOW *pWindows[MAX_WINDOW_COUNT];
  180.  
  181. WINHANDLE ahActiveWindows[MAX_WINDOW_COUNT];
  182. unsigned short usActiveWindowTop;
  183.  
  184. /* This Is THe Current State Of The Screen */
  185. short sCurrentX;
  186. short sCurrentY;
  187. enum eWinColors eCurrentForeAttr;
  188. enum eWinColors eCurrentBackAttr;
  189.  
  190. #if ANSI_SEQ
  191.  
  192.     #include <stdio.h>
  193.  
  194.     unsigned short usColor[eWinColorCount] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  195.  
  196.     void vTTYGotoXY(unsigned short usX, unsigned short usY)
  197.     {
  198.     fprintf( stdout,"%c[%d;%df",(char) 27, usY+1 , usX+1);
  199.     }
  200.  
  201.     void vTTYSetColor( enum eWinColors eForeAttr, enum eWinColors eBackAttr )
  202.     {
  203.     fprintf(stdout,"%c[%d;%dm",(char) 27, usColor[eForeAttr] + 30, usColor[eBackAttr] + 40);
  204.     }
  205.  
  206.     void vTTYPutc ( char cChar )
  207.     {
  208.     fputc(cChar, stdout);
  209.     }
  210.  
  211.     void vTTYInit(void)
  212.     {
  213.     }
  214.  
  215.     void vTTYEnd(void)
  216.     {
  217.     }
  218.  
  219. #elif OS2_V12
  220.  
  221.     #define INCL_SUB
  222.     #include <os2.h>
  223.  
  224.     struct OS2_CHAR_CELL
  225.     {
  226.     char    cChar;
  227.     char    cAttr;
  228.     };
  229.  
  230.     PBYTE            pbLVB;
  231.  
  232.     struct OS2_CHAR_CELL *  pCursor;
  233.     unsigned short        usLVB;
  234.     char            cCurAttr;
  235.  
  236.     void vTTYInit(void)
  237.     {
  238.     VioGetBuf( (PULONG) &pbLVB, (PUSHORT) &usLVB, 0);
  239.     }
  240.  
  241.     void vTTYEnd(void)
  242.     {
  243.     }
  244.  
  245.     void vTTYGotoXY(unsigned short usX, unsigned short usY)
  246.     {
  247.     pCursor = (struct OS2_CHAR_CELL *) pbLVB + usX + usY * usScreenWidth;
  248.     }
  249.  
  250.     void vTTYPutc ( char cChar )
  251.     {
  252.  
  253.     pCursor->cChar = cChar;
  254.     pCursor->cAttr = cCurAttr;
  255.  
  256.     VioShowBuf ( (USHORT) ( (PBYTE) pCursor - pbLVB), sizeof(struct OS2_CHAR_CELL), 0);
  257.     pCursor += 1;
  258.  
  259.     }
  260.  
  261.     void vTTYSetColor( enum eWinColors eForeAttr, enum eWinColors eBackAttr )
  262.     {
  263.     cCurAttr = (char) (eForeAttr + eBackAttr * 16);
  264.     }
  265.  
  266.  
  267. #elif DOS_BIOS
  268.  
  269.     #include <dos.h>
  270.     char        cCurAttr;
  271.     unsigned short  usCurX;
  272.     unsigned short  usCurY;
  273.  
  274.     void vTTYInit(void)
  275.     {
  276.     }
  277.  
  278.     void vTTYEnd(void)
  279.     {
  280.     }
  281.  
  282.     void vTTYGotoXY(unsigned short usX, unsigned short usY)
  283.     {
  284.     union REGS  regs;
  285.  
  286.     regs.h.ah = 0x02;    /* Function Type */
  287.     regs.h.bh = 0x00;    /* Current Page  */
  288.     regs.h.dh = (char) usY;    /* Row         */
  289.     regs.h.dl = (char) usX;    /* Column     */
  290.     int86 ( 0x10, ®s, ®s);
  291.     usCurX = usX;
  292.     usCurY = usY;
  293.     }
  294.  
  295.     void vTTYPutc ( char cChar )
  296.     {
  297.  
  298.     union REGS  regs;
  299.  
  300.     regs.h.ah = 0x09;    /* Function Type */
  301.     regs.h.al = cChar;    /* Char to Print */
  302.     regs.h.bh = 0x00;    /* Current Page  */
  303.     regs.h.bl = cCurAttr;    /* Attr to Use     */
  304.     regs.x.cx = 1;        /* Count     */
  305.  
  306.     int86 ( 0x10, ®s, ®s);
  307.  
  308.     vTTYGotoXY( usCurX+1, usCurY);
  309.     }
  310.  
  311.     void vTTYSetColor( enum eWinColors eForeAttr, enum eWinColors eBackAttr )
  312.     {
  313.     cCurAttr = (char) (eForeAttr + eBackAttr * 16);
  314.     }
  315.  
  316. #elif DOS_DIRECT
  317.  
  318.     #include <dos.h>
  319.  
  320.     char far *    pcVideoBase;
  321.     char far *    pCursor;
  322.     char    cCurAttr;
  323.  
  324.     void vTTYInit(void)
  325.     {
  326.     union REGS  regs;
  327.  
  328.     regs.h.ah = 0x0F;    /* Function Type (Get Video Mode) */
  329.     int86 ( 0x10, ®s, ®s);
  330.  
  331.     switch( regs.h.al)
  332.     {
  333.         case 7:
  334.             pcVideoBase = (char far *) 0xb0000000;
  335.             break;
  336.         default:
  337.             pcVideoBase = (char far *) 0xb8000000;
  338.             break;
  339.     }
  340.     }
  341.  
  342.     void vTTYGotoXY(unsigned short usX, unsigned short usY)
  343.     {
  344.     pCursor = pcVideoBase + 2 * (usX + usY * usScreenWidth);
  345.     }
  346.  
  347.     void vTTYPutc ( char cChar )
  348.     {
  349.  
  350.     *(pCursor++) = cChar;
  351.     *(pCursor++) = cCurAttr;
  352.     }
  353.  
  354.     void vTTYSetColor( enum eWinColors eForeAttr, enum eWinColors eBackAttr )
  355.     {
  356.     cCurAttr = (char) (eForeAttr + eBackAttr * 16);
  357.     }
  358.  
  359.     void vTTYEnd(void)
  360.     {
  361.     }
  362.  
  363. #else
  364.  
  365.     #error Must Select Curses Routines
  366.  
  367. #endif
  368.  
  369. int IsWinHandleValid (WINHANDLE hWin)
  370. {
  371.     return
  372.     (
  373.     hWin < MAX_WINDOW_COUNT &&
  374.     pWindows[hWin] != NULL
  375.     );
  376. }
  377.  
  378. unsigned short usWinGetBackGroundZ(void)
  379. {
  380.     return usActiveWindowTop;
  381. }
  382.  
  383. int IsXYOverLapping ( short sTestX, short sTestY, short sAreaX, short sAreaY, unsigned short usAreaW, unsigned short usAreaL)
  384. {
  385.     return
  386.     (
  387.     sTestX >= sAreaX && sTestX < sAreaX + usAreaW &&
  388.     sTestY >= sAreaY && sTestY < sAreaY + usAreaL
  389.     );
  390. }
  391.  
  392. int IsAreaOverLapping ( short sTestX, short sTestY, unsigned short usTestW, unsigned short usTestL, short sAreaX, short sAreaY, unsigned short usAreaW, unsigned short usAreaL)
  393. {
  394.     return
  395.     (
  396.     IsXYOverLapping ( sTestX      , sTestY        , sAreaX, sAreaY, usAreaW, usAreaL) ||
  397.     IsXYOverLapping ( sTestX + usTestW, sTestY        , sAreaX, sAreaY, usAreaW, usAreaL) ||
  398.     IsXYOverLapping ( sTestX      , sTestY + usTestL, sAreaX, sAreaY, usAreaW, usAreaL) ||
  399.     IsXYOverLapping ( sTestX + usTestW, sTestY + usTestL, sAreaX, sAreaY, usAreaW, usAreaL)
  400.     );
  401. }
  402.  
  403. short sWinGetOrgX(WINHANDLE hWin)
  404. {
  405.     if (IsWinHandleValid(hWin))
  406.     {
  407.     return pWindows[hWin]->sOrigX;
  408.     }
  409.     else
  410.     {
  411.     return 0;
  412.     }
  413. }
  414.  
  415. short sWinGetOrgY(WINHANDLE hWin)
  416. {
  417.     if (IsWinHandleValid(hWin))
  418.     {
  419.     return pWindows[hWin]->sOrigY;
  420.     }
  421.     else
  422.     {
  423.     return 0;
  424.     }
  425. }
  426.  
  427. short sWinGetCurX(WINHANDLE hWin)
  428. {
  429.     if (IsWinHandleValid(hWin))
  430.     {
  431.     return pWindows[hWin]->sCursX;
  432.     }
  433.     else
  434.     {
  435.     return 0;
  436.     }
  437. }
  438.  
  439. short sWinGetCurY(WINHANDLE hWin)
  440. {
  441.     if (IsWinHandleValid(hWin))
  442.     {
  443.     return pWindows[hWin]->sCursY;
  444.     }
  445.     else
  446.     {
  447.     return 0;
  448.     }
  449. }
  450.  
  451. short sWinGetLocX(WINHANDLE hWin)
  452. {
  453.     if (IsWinHandleValid(hWin))
  454.     {
  455.     return pWindows[hWin]->sPhysX;
  456.     }
  457.     else
  458.     {
  459.     return 0;
  460.     }
  461. }
  462.  
  463. short sWinGetLocY(WINHANDLE hWin)
  464. {
  465.     if (IsWinHandleValid(hWin))
  466.     {
  467.     return pWindows[hWin]->sPhysY;
  468.     }
  469.     else
  470.     {
  471.     return 0;
  472.     }
  473. }
  474.  
  475. unsigned short usWinGetLocZ(WINHANDLE hWin)
  476. {
  477.     unsigned short usIndex;
  478.     if (IsWinHandleValid(hWin))
  479.     {
  480.     usIndex = 0;
  481.     while
  482.     (
  483.         usIndex < usActiveWindowTop &&
  484.         ahActiveWindows[usIndex] != hWin
  485.     )
  486.         usIndex += 1;
  487.     }
  488.  
  489.     return usIndex;
  490. }
  491.  
  492. unsigned short usWinGetLocW(WINHANDLE hWin)
  493. {
  494.     if (IsWinHandleValid(hWin))
  495.     {
  496.     return pWindows[hWin]->usViewW;
  497.     }
  498.     else
  499.     {
  500.     return 0;
  501.     }
  502. }
  503.  
  504. unsigned short usWinGetLocL(WINHANDLE hWin)
  505. {
  506.     if (IsWinHandleValid(hWin))
  507.     {
  508.     return pWindows[hWin]->usViewL;
  509.     }
  510.     else
  511.     {
  512.     return 0;
  513.     }
  514. }
  515.  
  516. void vWinPutCellAt(short sX, short sY, char character, enum eWinColors eForeAttr, enum eWinColors eBackAttr)
  517. {
  518.     if
  519.     (
  520.     eCurrentForeAttr != eForeAttr ||
  521.     eCurrentBackAttr != eBackAttr
  522.     )
  523.     {
  524.     vTTYSetColor( eForeAttr, eBackAttr);
  525.     eCurrentForeAttr = eForeAttr;
  526.     eCurrentBackAttr = eBackAttr;
  527.     }
  528.  
  529.     if
  530.     (
  531.     sX >= sScreenOriginX            &&
  532.     sY >= sScreenOriginY            &&
  533.     (unsigned short)(sX - sScreenOriginX) < usScreenWidth    &&
  534.     (unsigned short)(sY - sScreenOriginY) < usScreenLength
  535.     )
  536.     {
  537.     if
  538.     (
  539.         sCurrentX != sX + sScreenOriginX ||
  540.         sCurrentY != sY + sScreenOriginY
  541.     )
  542.     {
  543.         sCurrentX = sX + sScreenOriginX;
  544.         sCurrentY = sY + sScreenOriginY;
  545.         vTTYGotoXY ( (unsigned short) sCurrentX, (unsigned short) sCurrentY);
  546.     }
  547.  
  548.     vTTYPutc ( character );
  549.     sCurrentX += 1;
  550.     }
  551.  
  552. }
  553.  
  554. int IsWinCharActive(short sX, short sY, unsigned short usZ)
  555. {
  556.     unsigned short usIndex;
  557.     int IsValid;
  558.  
  559.     IsValid = ( 1==0 );
  560.  
  561.     if ( usZ <= usWinGetBackGroundZ() )
  562.     {
  563.     IsValid = ( 1==1 );
  564.     for ( usIndex = 0; usIndex < usZ && IsValid; ++usIndex)
  565.     {
  566.         IsValid &= !IsXYOverLapping
  567.         (
  568.         sX, sY,
  569.         pWindows[ahActiveWindows[usIndex]]->sPhysX,
  570.         pWindows[ahActiveWindows[usIndex]]->sPhysY,
  571.         pWindows[ahActiveWindows[usIndex]]->usPhysW,
  572.         pWindows[ahActiveWindows[usIndex]]->usPhysL
  573.         );
  574.     }
  575.     }
  576.  
  577.     return IsValid;
  578. }
  579.  
  580. void vWinInvalidateRec( short sX, short sY, unsigned short usW, unsigned short usL)
  581. {
  582.     unsigned short  usTopZ;
  583.     short  sTmpX;
  584.     short  sTmpY;
  585.     unsigned short  sTmpZ;
  586.  
  587.     unsigned char ucChar;
  588.     int fFoundFlag;
  589.     enum eWinColors eForeAttr;
  590.     enum eWinColors eBackAttr;
  591.     struct CHARCELL *pCurCell;
  592.     struct WINDOW *pCurWindow;
  593.  
  594.     usTopZ = usWinGetBackGroundZ();
  595.  
  596.     for ( sTmpY = sY; sTmpY < sY + (signed short) usL; ++sTmpY)
  597.     {
  598.     for ( sTmpX = sX; sTmpX < sX + (signed short) usW; ++sTmpX)
  599.     {
  600.         ucChar = ucBackGroundChar;
  601.         eForeAttr = eForeBackGrnd;
  602.         eBackAttr = eBackBackGrnd;
  603.         fFoundFlag = (1==0);
  604.         for
  605.         (
  606.         sTmpZ = 0;
  607.         sTmpZ < usTopZ && !fFoundFlag;
  608.         ++sTmpZ
  609.         )
  610.         {
  611.         pCurWindow = pWindows[ahActiveWindows[sTmpZ]];
  612.         if
  613.         (
  614.             IsXYOverLapping
  615.             (
  616.             sTmpX,
  617.             sTmpY,
  618.             pCurWindow->sViewX,
  619.             pCurWindow->sViewY,
  620.             pCurWindow->usViewW,
  621.             pCurWindow->usViewL
  622.             )
  623.         )
  624.         {
  625.             fFoundFlag = (1==1);
  626.             if
  627.             (
  628.             sTmpY + pCurWindow->sOrigY - pCurWindow->sViewY >= 0 &&
  629.             (unsigned short) sTmpY + pCurWindow->sOrigY - pCurWindow->sViewY < pCurWindow->usLogcL &&
  630.             sTmpX + pCurWindow->sOrigX - pCurWindow->sViewX >= 0 &&
  631.             (unsigned short) sTmpX + pCurWindow->sOrigX - pCurWindow->sViewX < pCurWindow->usLogcW
  632.             )
  633.             {
  634.             pCurCell = pCurWindow->pData + (sTmpY + pCurWindow->sOrigY - pCurWindow->sViewY) * pCurWindow->usLogcW + sTmpX + pCurWindow->sOrigX - pCurWindow->sViewX;
  635.             ucChar = pCurCell->ucChar;
  636.             eForeAttr = pCurCell->eForeAttr;
  637.             eBackAttr = pCurCell->eBackAttr;
  638.             }
  639.         }
  640.         else if ( pCurWindow->ulFlags & FRAME_SWITCH)
  641.         {
  642.             if (sTmpY == pCurWindow->sPhysY)
  643.             {
  644.             if ( sTmpX == pCurWindow->sPhysX )
  645.             {
  646.                 eForeAttr = pCurWindow->eForeBord;
  647.                 eBackAttr = pCurWindow->eBackBord;
  648.                 ucChar = UPPER_LEFT_CHAR;
  649.                 fFoundFlag = (1==1);
  650.             }
  651.             else if ( sTmpX == pCurWindow->sPhysX + pCurWindow->usPhysW - 1)
  652.             {
  653.                 eForeAttr = pCurWindow->eForeBord;
  654.                 eBackAttr = pCurWindow->eBackBord;
  655.                 ucChar = UPPER_RIGHT_CHAR;
  656.                 fFoundFlag = (1==1);
  657.             }
  658.             else if ( sTmpX > pCurWindow->sPhysX && sTmpX < pCurWindow->sPhysX + (signed short) pCurWindow->usPhysW - 1)
  659.             {
  660.                 eForeAttr = pCurWindow->eForeBord;
  661.                 eBackAttr = pCurWindow->eBackBord;
  662.                 ucChar = HORIZONTAL_CHAR;
  663.                 fFoundFlag = (1==1);
  664.             }
  665.  
  666.             }
  667.             else if (sTmpY == pCurWindow->sPhysY + pCurWindow->usPhysL - 1)
  668.             {
  669.             if ( sTmpX == pCurWindow->sPhysX )
  670.             {
  671.                 eForeAttr = pCurWindow->eForeBord;
  672.                 eBackAttr = pCurWindow->eBackBord;
  673.                 ucChar = LOWER_LEFT_CHAR;
  674.                 fFoundFlag = (1==1);
  675.             }
  676.             else if ( sTmpX == pCurWindow->sPhysX + pCurWindow->usPhysW - 1)
  677.             {
  678.                 eForeAttr = pCurWindow->eForeBord;
  679.                 eBackAttr = pCurWindow->eBackBord;
  680.                 ucChar = LOWER_RIGHT_CHAR;
  681.                 fFoundFlag = (1==1);
  682.             }
  683.             else if ( sTmpX > pCurWindow->sPhysX && sTmpX < pCurWindow->sPhysX + (signed short) pCurWindow->usPhysW - 1)
  684.             {
  685.                 eForeAttr = pCurWindow->eForeBord;
  686.                 eBackAttr = pCurWindow->eBackBord;
  687.                 ucChar = HORIZONTAL_CHAR;
  688.                 fFoundFlag = (1==1);
  689.             }
  690.  
  691.             }
  692.             else if
  693.             (
  694.             sTmpY > pCurWindow->sPhysY &&
  695.             sTmpY < pCurWindow->sPhysY + (signed short) pCurWindow->usPhysL - 1 &&
  696.             (
  697.                 sTmpX == pCurWindow->sPhysX ||
  698.                 sTmpX == pCurWindow->sPhysX + pCurWindow->usPhysW - 1
  699.             )
  700.             )
  701.             {
  702.             eForeAttr = pCurWindow->eForeBord;
  703.             eBackAttr = pCurWindow->eBackBord;
  704.             ucChar = VERTICAL_CHAR;
  705.             fFoundFlag = (1==1);
  706.             }
  707.         }
  708.         }
  709.  
  710.         vWinPutCellAt(sTmpX, sTmpY, ucChar, eForeAttr, eBackAttr);
  711.     }
  712.     }
  713. }
  714.  
  715.  
  716.  
  717. void vWinInvalidate( WINHANDLE hWin )
  718. {
  719.  
  720.     vWinInvalidateRec
  721.     (
  722.     pWindows[hWin]->sPhysX,
  723.     pWindows[hWin]->sPhysY,
  724.     pWindows[hWin]->usPhysW,
  725.     pWindows[hWin]->usPhysL
  726.     );
  727.  
  728. }
  729.  
  730. void vWinDeleteZ(unsigned short usPos)
  731. {
  732.     unsigned short usIndex;
  733.     short sX;
  734.     short sY;
  735.     unsigned short usW;
  736.     unsigned short usL;
  737.  
  738.     if (usPos < usActiveWindowTop)
  739.     {
  740.  
  741.     sX = pWindows[ahActiveWindows[usPos]]->sPhysX;
  742.     sY = pWindows[ahActiveWindows[usPos]]->sPhysY;
  743.     usW = pWindows[ahActiveWindows[usPos]]->usPhysW;
  744.     usL = pWindows[ahActiveWindows[usPos]]->usPhysL;
  745.  
  746.     for ( usIndex = usPos; usIndex < usActiveWindowTop; ++usIndex)
  747.     {
  748.         ahActiveWindows[usIndex] = ahActiveWindows[usIndex+1];
  749.     }
  750.     ahActiveWindows[usActiveWindowTop] = NULL_WINDOW;
  751.     usActiveWindowTop -= 1;
  752.  
  753.     }
  754. }
  755.  
  756. void vWinInsertAtZ(unsigned short usPos, WINHANDLE hWin)
  757. {
  758.     unsigned short usIndex;
  759.     if
  760.     (
  761.     hWin < MAX_WINDOW_COUNT     &&
  762.     IsWinHandleValid(hWin)        &&
  763.     usPos <= usActiveWindowTop
  764.     )
  765.     {
  766.     for ( usIndex = usActiveWindowTop; usIndex > usPos; --usIndex)
  767.     {
  768.         ahActiveWindows[usIndex] = ahActiveWindows[usIndex-1];
  769.     }
  770.         usActiveWindowTop += 1;
  771.  
  772.     ahActiveWindows[usPos] = hWin;
  773.  
  774.     }
  775.  
  776. }
  777.  
  778. void vWinSetLocZ (WINHANDLE hWin, unsigned short usPos)
  779. {
  780.  
  781.     if
  782.     (
  783.     hWin < MAX_WINDOW_COUNT             &&
  784.     IsWinHandleValid(hWin)                &&
  785.     usPos <= usActiveWindowTop            &&
  786.     pWindows[hWin]->ulFlags & ACTIVATED_SWITCH
  787.     )
  788.     {
  789.     vWinDeleteZ(usWinGetLocZ(hWin));
  790.     vWinInsertAtZ(usPos, hWin);
  791.     vWinInvalidate( hWin );
  792.     }
  793. }
  794.  
  795. void vWinSetLocXY ( WINHANDLE hWin, short sX, short sY)
  796. {
  797.     short sSaveX, sSaveY;
  798.  
  799.     if ( IsWinHandleValid ( hWin ) )
  800.     {
  801.     sSaveX = pWindows[hWin]->sPhysX;
  802.     sSaveY = pWindows[hWin]->sPhysY;
  803.  
  804.     pWindows[hWin]->sPhysX = sX;
  805.     pWindows[hWin]->sPhysY = sY;
  806.     pWindows[hWin]->sViewX = sX;
  807.     pWindows[hWin]->sViewY = sY;
  808.  
  809.     if ( pWindows[hWin]->ulFlags & FRAME_SWITCH )
  810.     {
  811.         pWindows[hWin]->sViewX += 1;
  812.         pWindows[hWin]->sViewY += 1;
  813.     }
  814.  
  815.     if ( pWindows[hWin]->ulFlags & ACTIVATED_SWITCH )
  816.     {
  817.         vWinInvalidateRec
  818.         (
  819.         sSaveX,
  820.         sSaveY,
  821.         pWindows[hWin]->usPhysW,
  822.         pWindows[hWin]->usPhysL
  823.         );
  824.  
  825.         vWinInvalidate (hWin);
  826.     }
  827.     }
  828.  
  829. }
  830.  
  831. void vWinSetLocWL ( WINHANDLE hWin, unsigned short usW, unsigned short usL)
  832. {
  833.  
  834.     unsigned short usSaveW, usSaveL;
  835.  
  836.     if (IsWinHandleValid ( hWin ))
  837.     {
  838.     /*
  839.         Note: since usW & usL are unsigned This Check will also fail
  840.         if a negative number is sent
  841.     */
  842.     if
  843.     (
  844.         usW <= pWindows[hWin]->usLogcW &&
  845.         usL <= pWindows[hWin]->usLogcL
  846.     )
  847.     {
  848.         usSaveW = pWindows[hWin]->usPhysW;
  849.         usSaveL = pWindows[hWin]->usPhysL;
  850.  
  851.         pWindows[hWin]->usViewW = usW;
  852.         pWindows[hWin]->usViewL = usL;
  853.         pWindows[hWin]->usPhysW = usW;
  854.         pWindows[hWin]->usPhysL = usL;
  855.  
  856.         if ( pWindows[hWin]->ulFlags & FRAME_SWITCH )
  857.         {
  858.         pWindows[hWin]->usPhysW += 2;
  859.         pWindows[hWin]->usPhysL += 2;
  860.         }
  861.  
  862.         if ( pWindows[hWin]->ulFlags & ACTIVATED_SWITCH )
  863.         {
  864.         vWinInvalidateRec
  865.         (
  866.             pWindows[hWin]->sPhysX,
  867.             pWindows[hWin]->sPhysY,
  868.             usSaveW,
  869.             usSaveL
  870.         );
  871.  
  872.         vWinInvalidate (hWin);
  873.         }
  874.     }
  875.     }
  876. }
  877.  
  878. void vWinSetCurXY( WINHANDLE hWin, short sX, short sY)
  879. {
  880.     if ( IsWinHandleValid ( hWin ) )
  881.     {
  882.     pWindows[hWin]->sCursX = sX;
  883.     pWindows[hWin]->sCursY = sY;
  884.  
  885.     }
  886. }
  887.  
  888.  
  889. void vWinSetOrgXY ( WINHANDLE hWin, short sX, short sY)
  890. {
  891.     if ( IsWinHandleValid ( hWin ) )
  892.     {
  893.     pWindows[hWin]->sOrigX = sX;
  894.     pWindows[hWin]->sOrigY = sY;
  895.  
  896.     vWinInvalidate(hWin);
  897.     }
  898.  
  899. }
  900.  
  901. void vWinActivate(WINHANDLE hWin)
  902. {
  903.     if
  904.     (
  905.     IsWinHandleValid(hWin)    &&
  906.     (pWindows[hWin]->ulFlags & ACTIVATED_SWITCH) != ACTIVATED_SWITCH
  907.     )
  908.     {
  909.     pWindows[hWin]->ulFlags |= ACTIVATED_SWITCH;
  910.     vWinInsertAtZ(0, hWin);
  911.     vWinInvalidate( hWin );
  912.     }
  913. }
  914.  
  915. void vWinDeactivate(WINHANDLE hWin)
  916. {
  917.     if
  918.     (
  919.     IsWinHandleValid(hWin) &&
  920.     pWindows[hWin]->ulFlags & ACTIVATED_SWITCH
  921.     )
  922.     {
  923.  
  924.     pWindows[hWin]->ulFlags &= ~ACTIVATED_SWITCH;
  925.     vWinDeleteZ(usWinGetLocZ(hWin));
  926.     vWinInvalidate( hWin );
  927.     }
  928. }
  929.  
  930. void vWinOpen (short sX, short sY, unsigned char ucBackChar, enum eWinColors eForeGrnd, enum eWinColors eBackGrnd)
  931. {
  932.  
  933.     unsigned short usIndex;
  934.  
  935.     usActiveWindowTop = 0;
  936.     for
  937.     (
  938.     usIndex = 0;
  939.     usIndex < MAX_WINDOW_COUNT;
  940.     ++usIndex
  941.     )
  942.     {
  943.     pWindows[usIndex] = NULL;
  944.     ahActiveWindows[usIndex] = NULL_WINDOW;
  945.     }
  946.     ucBackGroundChar = ucBackChar;
  947.     eForeBackGrnd = eForeGrnd;
  948.     eBackBackGrnd = eBackGrnd;
  949.     eForeShadow = eWinWhite;
  950.     eBackShadow = eWinBlack;
  951.  
  952.     sScreenOriginX = 0;
  953.     sScreenOriginY = 0;
  954.     usScreenWidth = sX;
  955.     usScreenLength= sY;
  956.  
  957.     vTTYInit();
  958.     vTTYGotoXY ( 0, 0);
  959.     vTTYSetColor( eWinWhite, eWinBlack);
  960.  
  961.     vWinInvalidateRec ( 0, 0, usScreenWidth, usScreenLength-1);
  962.  
  963. }
  964.  
  965. void vWinClose(void)
  966. {
  967.     vTTYEnd();
  968. }
  969.  
  970. WINHANDLE hWinFindNextFree( WINHANDLE hStartWith )
  971. {
  972.     WINHANDLE hWin;
  973.     for
  974.     (
  975.     hWin = hStartWith;
  976.     hWin < MAX_WINDOW_COUNT &&
  977.     pWindows[hWin] != NULL;
  978.     ++hWin
  979.     )
  980.     ;
  981.  
  982.     return hWin;
  983.  
  984. }
  985.  
  986. void vWinClear( WINHANDLE hWin )
  987. {
  988.     struct CHARCELL *pCharCell;
  989.     short sX;
  990.     short sY;
  991.  
  992.     if ( IsWinHandleValid ( hWin ) )
  993.     {
  994.     vWinSetCurXY( hWin, 0, 0);
  995.     vWinSetOrgXY ( hWin, 0, 0);
  996.  
  997.     for ( sY = 0; (unsigned short) sY < pWindows[hWin]->usLogcL; ++sY)
  998.         for ( sX = 0; (unsigned short) sX < pWindows[hWin]->usLogcW; ++sX)
  999.         {
  1000.         pCharCell = (pWindows[hWin]->pData + sX + sY * pWindows[hWin]->usLogcW);
  1001.         pCharCell->ucChar = pWindows[hWin]->ucBackGroundChar;
  1002.         pCharCell->eForeAttr = pWindows[hWin]->eForeAttr;
  1003.         pCharCell->eBackAttr = pWindows[hWin]->eBackAttr;
  1004.         }
  1005.     vWinInvalidate(hWin);
  1006.     }
  1007. }
  1008.  
  1009. void vWinSetTextColor(WINHANDLE hWin, enum eWinColors eForeAttr, enum eWinColors eBackAttr)
  1010. {
  1011.     if ( IsWinHandleValid ( hWin ) )
  1012.     {
  1013.     pWindows[hWin]->eForeAttr = eForeAttr;
  1014.     pWindows[hWin]->eBackAttr = eBackAttr;
  1015.     }
  1016.  
  1017. }
  1018.  
  1019. void vWinSetBorderColor(WINHANDLE hWin, enum eWinColors eForeAttr, enum eWinColors eBackAttr)
  1020. {
  1021.     if ( IsWinHandleValid ( hWin ) )
  1022.     {
  1023.     pWindows[hWin]->eForeBord = eForeAttr;
  1024.     pWindows[hWin]->eBackBord = eBackAttr;
  1025.  
  1026.     if ( pWindows[hWin]->ulFlags & ACTIVATED_SWITCH )
  1027.         vWinInvalidate(hWin);
  1028.     }
  1029.  
  1030. }
  1031.  
  1032.  
  1033. WINHANDLE vWinCreate (unsigned short sPhysX, unsigned short sPhysY, unsigned short usViewW, unsigned short usViewL, unsigned short usLogcW, unsigned short usLogcL, unsigned long ulFlags)
  1034. {
  1035.     WINHANDLE hWin;
  1036.     hWin = hWinFindNextFree( (WINHANDLE) 0 );
  1037.     if ( hWin != NULL_WINDOW )
  1038.     {
  1039.     pWindows[hWin] = malloc ( sizeof(struct WINDOW) );
  1040.     if( pWindows[hWin] == NULL )
  1041.     {
  1042.         hWin = NULL_WINDOW;
  1043.     }
  1044.     else
  1045.     {
  1046.         pWindows[hWin]->ulFlags = ulFlags;
  1047.         pWindows[hWin]->usLogcW = usLogcW;
  1048.         pWindows[hWin]->usLogcL = usLogcL;
  1049.         pWindows[hWin]->usTabStop = DEFAULT_TAB;
  1050.         pWindows[hWin]->ucBackGroundChar = DEFAULT_BACKGROUNDCHAR;
  1051.         pWindows[hWin]->pData = malloc( sizeof(struct CHARCELL) * usLogcW * usLogcL);
  1052.  
  1053.         if ( pWindows[hWin]->pData == NULL )
  1054.         {
  1055.         free ( pWindows[hWin] );
  1056.         hWin = NULL_WINDOW;
  1057.         }
  1058.         else
  1059.         {
  1060.             vWinSetTextColor( hWin, eWinWhite, eWinBlack);
  1061.             vWinSetBorderColor( hWin, eWinWhite, eWinBlack);
  1062.             vWinSetLocXY ( hWin, sPhysX, sPhysY);
  1063.             vWinSetLocWL ( hWin, usViewW, usViewL);
  1064.             vWinSetOrgXY ( hWin, 0, 0);
  1065.             vWinSetCurXY( hWin, 0, 0);
  1066.  
  1067.             vWinClear( hWin );
  1068.         }
  1069.  
  1070.     }
  1071.     }
  1072.  
  1073.     return hWin;
  1074.  
  1075. }
  1076.  
  1077. void vWinDestroy(WINHANDLE hWin)
  1078. {
  1079.     if (pWindows[hWin] != NULL)
  1080.     {
  1081.     vWinDeactivate(hWin);
  1082.     free(pWindows[hWin]);
  1083.     pWindows[hWin] = NULL;
  1084.     }
  1085. }
  1086.  
  1087. void vWinScrollDown(WINHANDLE hWin, unsigned short usCount)
  1088. {
  1089.     struct CHARCELL *pFromCell;
  1090.     struct CHARCELL *pToCell;
  1091.     unsigned short usIndex;
  1092.     if ( IsWinHandleValid ( hWin ) )
  1093.     {
  1094.     pToCell = pWindows[hWin]->pData;
  1095.     pFromCell = (pWindows[hWin]->pData + usCount * pWindows[hWin]->usLogcW);
  1096.     usIndex = 0;
  1097.     while ( usIndex < (pWindows[hWin]->usLogcL - usCount) * pWindows[hWin]->usLogcW)
  1098.     {
  1099.         memcpy
  1100.         (
  1101.           pToCell + usIndex,
  1102.         pFromCell + usIndex,
  1103.         sizeof(struct CHARCELL)
  1104.         );
  1105.         ++usIndex;
  1106.     }
  1107.  
  1108.     while ( usIndex < pWindows[hWin]->usLogcL * pWindows[hWin]->usLogcW )
  1109.     {
  1110.         (pToCell + usIndex)->ucChar = ' ';
  1111.         (pToCell + usIndex)->eForeAttr = pWindows[hWin]->eForeAttr;
  1112.         (pToCell + usIndex)->eBackAttr = pWindows[hWin]->eBackAttr;
  1113.          ++usIndex;
  1114.     }
  1115.  
  1116.     if ( pWindows[hWin]->ulFlags & ACTIVATED_SWITCH )
  1117.         vWinInvalidate(hWin);
  1118.     }
  1119. }
  1120.  
  1121. void vWinCurUpdate(WINHANDLE hWin)
  1122. {
  1123.     if ( IsWinHandleValid ( hWin ) )
  1124.     {
  1125.     if ( (unsigned short) pWindows[hWin]->sCursX >= pWindows[hWin]->usLogcW )
  1126.     {
  1127.         pWindows[hWin]->sCursX = 0;
  1128.         pWindows[hWin]->sCursY += 1;
  1129.     }
  1130.  
  1131.     if ( (unsigned short) pWindows[hWin]->sCursY >= pWindows[hWin]->usLogcL )
  1132.     {
  1133.         vWinScrollDown( hWin, pWindows[hWin]->sCursY - pWindows[hWin]->usLogcL + 1);
  1134.         pWindows[hWin]->sCursY = pWindows[hWin]->usLogcL - 1;
  1135.     }
  1136.  
  1137.     }
  1138. }
  1139.  
  1140. void WinSetCharAt(short sX, short sY, unsigned short usZ, char character, enum eWinColors eForeAttr, enum eWinColors eBackAttr)
  1141. {
  1142.     if
  1143.     (IsWinCharActive(sX, sY, usZ))
  1144.     {
  1145.     vWinPutCellAt(sX, sY, character, eForeAttr, eBackAttr);
  1146.     }
  1147. }
  1148.  
  1149. int IsWinPosVisible(WINHANDLE hWin, short sX, short sY)
  1150. {
  1151.     if ( IsWinHandleValid ( hWin ) )
  1152.     {
  1153.     return
  1154.     (
  1155.         pWindows[hWin]->ulFlags & ACTIVATED_SWITCH &&
  1156.         sX >= pWindows[hWin]->sOrigX &&
  1157.         sY >= pWindows[hWin]->sOrigY &&
  1158.         (unsigned short) sX - pWindows[hWin]->sOrigX < pWindows[hWin]->usViewW &&
  1159.         (unsigned short) sY - pWindows[hWin]->sOrigY < pWindows[hWin]->usViewL
  1160.     );
  1161.     }
  1162.     else
  1163.     {
  1164.     return ( 1==0 );
  1165.     }
  1166.  
  1167. }
  1168.  
  1169. void vWinPutC (WINHANDLE hWin, char cChar)
  1170. {
  1171.     struct CHARCELL *pCharCell;
  1172.  
  1173.     if ( IsWinHandleValid ( hWin ) )
  1174.     {
  1175.     switch(cChar)
  1176.     {
  1177.         /* BackSpace*/
  1178.         case '\b':
  1179.             if (pWindows[hWin]->sCursX > 0)
  1180.             --pWindows[hWin]->sCursX;
  1181.             break;
  1182.  
  1183.         /* Tab */
  1184.         case '\t':
  1185.             pWindows[hWin]->sCursX += pWindows[hWin]->sCursX % pWindows[hWin]->usTabStop;
  1186.             break;
  1187.  
  1188.         /* LineFeed */
  1189.         case '\n':
  1190.             pWindows[hWin]->sCursY += 1;
  1191.             pWindows[hWin]->sCursX = 0;
  1192.             break;
  1193.  
  1194.         /* Return */
  1195.         case '\r':
  1196.             pWindows[hWin]->sCursX = 0;
  1197.             break;
  1198.  
  1199.         /* Bell */
  1200.         case '\a':
  1201.             break;
  1202.  
  1203.         default:
  1204.             if (IsWinPosVisible(hWin, pWindows[hWin]->sCursX, pWindows[hWin]->sCursY) )
  1205.             {
  1206.             WinSetCharAt
  1207.             (
  1208.                 pWindows[hWin]->sViewX + pWindows[hWin]->sCursX - pWindows[hWin]->sOrigX,
  1209.                 pWindows[hWin]->sViewY + pWindows[hWin]->sCursY - pWindows[hWin]->sOrigY,
  1210.                 usWinGetLocZ(hWin),
  1211.                 cChar,
  1212.                 pWindows[hWin]->eForeAttr,
  1213.                 pWindows[hWin]->eBackAttr
  1214.             );
  1215.             }
  1216.  
  1217.             pCharCell = (pWindows[hWin]->pData + pWindows[hWin]->sCursX + pWindows[hWin]->sCursY * pWindows[hWin]->usLogcW);
  1218.             pCharCell->ucChar = cChar;
  1219.             pCharCell->eForeAttr = pWindows[hWin]->eForeAttr;
  1220.             pCharCell->eBackAttr = pWindows[hWin]->eBackAttr;
  1221.  
  1222.             pWindows[hWin]->sCursX += 1;
  1223.             break;
  1224.     }
  1225.     vWinCurUpdate(hWin);
  1226.     }
  1227.  
  1228. }
  1229.  
  1230.  
  1231. void vWinPutS ( WINHANDLE hWin, char * pszString )
  1232. {
  1233.     char *pszTmp;
  1234.     pszTmp = pszString;
  1235.     while ( *pszTmp != '\0' )
  1236.     {
  1237.     vWinPutC ( hWin, *pszTmp);
  1238.     pszTmp += 1;
  1239.     }
  1240. }
  1241.